package com.qf.manager.controller;

import com.qf.manager.pojo.po.User;
import com.qf.manager.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping(value = "/{uid}",method = RequestMethod.GET)
    @ResponseBody
    public User getUserByUid(@PathVariable("uid") Long uid){
        User user=userService.getUserByUid(uid);
        return user;
    }
    @RequestMapping(value = "/",method = RequestMethod.GET)
    @ResponseBody
    public List<User> getUsers(){
        return userService.getUsers();
    }
    @RequestMapping(value = "/{uid}",method = RequestMethod.DELETE)
    @ResponseBody
    public Map<String,Object> deleteUserByUid(@PathVariable("uid")Long uid){
        int rows=userService.deleteUserByUid(uid);
        Map<String,Object> deleteFlag=new HashMap<>();
        boolean flag=false;
        if(rows!=0)
            flag=true;
        deleteFlag.put("flag",flag);
        return deleteFlag;
    }
    @RequestMapping(value = "/",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> postUser(User user){
        int rows=userService.insertUser(user);
        boolean flag=false;
        Map<String,Object> insertFlag=new HashMap<>();
        if(rows!=0)
            flag=true;
        insertFlag.put("flag",flag);
        return insertFlag;
    }
    @RequestMapping(value = "/",method = RequestMethod.PUT)
    @ResponseBody
    public Map<String,Object> putUserByUid(User user) {
        System.out.println(user);
        int rows=userService.updateUserByUid(user);
        boolean flag=false;
        Map<String,Object> updateFlag=new HashMap<>();
        if(rows!=0)
            flag=true;
        updateFlag.put("flag",flag);
        return updateFlag;
    }
}
